20. Switch Statement

Switch

A switch statement is very similar to an if clause. In fact, you can write a program that does the exact same thing with either a switch statement or a series of if-else clauses.

Then why bother using a switch statement? For reasons we won't detail here, switch statements can oftentimes be faster to execute. Many programming languages have a switch statement including Java, Javascript, PHP, C++ among others; Python is an exception.

Since there is no Python switch statement, we will compare a set of if-else C++ clauses with a C++ switch statement.

Click on the image to zoom in.

The output of the code would be

Not Moving - Neutral
Your car is currently in gear: N

Let's break down what is happening in the switch statement:

```c++
char gear_status = 'N';

switch(gear_status) {
    case 'D' :
        std::cout << "Driving Forward" << std::endl;
        break;
    case 'N' :
        std::cout << "Not Moving - Neutral" << std::endl;
        break;
    case 'P' :
        std::cout << "Not Moving - Parked" << std::endl;
        break;
    case 'R':
        std::cout << "Driving in Reverse" << std::endl;
        break;
}

```

Each time case appears, the code checks to see if the gear_status variables matches the case. Once gear_status finds a matching case, the code inside the case runs.

In C++, the switch statement was designed to run the code in the matching case and then all of the cases below. You need the break lines of code if you want your code to leave the switch after executing the matching case.

In other words, if the code were written without using break,

#include <iostream>

int main() {

    char gear_status = 'N';

    switch(gear_status) {
        case 'D' :
            std::cout << "Driving Forward" << std::endl;
        case 'N' :
            std::cout << "Not Moving - Neutral" << std::endl;
        case 'P' :
            std::cout << "Not Moving - Parked" << std::endl;
        case 'R':
            std::cout << "Driving in Reverse" << std::endl;
    }

    std::cout << "Your car is currently in gear: " << gear_status << std::endl;

    return 0;
}

the code would still skip the 'D' case. But once the code found a match with the 'N' case, the code in the 'N', 'P', and 'R' cases would execute.

Switch Limitations

If-else statements are much more flexible than switch statements. In fact, the case clauses in switch statements can only make comparisons between integer values. Switch cases can also compare characters like in the example code because C++ is actually converting the characters to integers.

On the other hand, if statements can make comparisons between floating point numbers as well as between integers.

The general form of a switch statement looks like this:

    int variable = integer;

    switch(variable) {
        case 1:
            code statements;
            break;
        case 2 :
            code statements;
            break;
        case 3:
            code statements;
            break;
        case 4:
            code statements;
            break;
        case etc ... 
    }

Switch Statement - Playground

Practice writing a switch statement in the playground below. The code comments will help you get started. You can run your code with the "Test Run" button and then compare your solution with "solution.cpp".

Start Quiz:

//TODO Practice writing switch statements
// Don't forget an include statement if you want to use std::cout

int main() {
    
    // TODO: write a program that outputs whether a vehicle is a motorcycle,
    // 2-door coupe, 4-door car or a 5-door mini-van. 
    // You should create a variable that holds the number of doors in the vehicle
    // A motorcycle would have doors = 0 for example. 
    // Then use a switch statement to output to the terminal the kind of vehicle
    // you have
    
    return 0;
}
//TODO Practice writing switch statements
// Don't forget an include statement if you want to use std::cout

#include <iostream>

int main() {
    
    // TODO: write a program that outputs whether a vehicle is a motorcycle,
    // 2-door coupe, 4-door car or a 5-door mini-van. 
    // You should create a variable that holds the number of doors in the vehicle
    // A motorcycle would have doors = 0 for example. 
    // Then use a switch statement to output to the terminal the kind of vehicle
    // you have

    int doors = 5;
    
    switch(doors) {
        case 0:
            std::cout << "Motorcycle" << std::endl;
            break;
        case 2:
            std::cout << "Coupe" << std::endl;
            break;
        case 4:
            std::cout << "Sedan" << std::endl;
            break;
        case 5:
            std::cout << "Mini-van" <<std::endl;
            break;

    }
    
    return 0;
}